//
//  MAMessageDetailCell.m
//  meiang_edu
//
//  Created by iloveko8e on 2018/9/15.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAMessageDetailHeaderView.h"
#import "MAActionView.h"
#import "MAMessageDetailModel.h"

#define WIDTH_SCALE  [UIScreen mainScreen].bounds.size.width / 375

@interface MAMessageDetailHeaderView ()

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *sendLabel;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UIView *imageContentView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) MAActionView *applyView;
@property (nonatomic, strong) MAActionView *adviceView;
@property (nonatomic, strong) NSArray *arrImage;
@property (nonatomic, assign) BOOL isService;

@end

@implementation MAMessageDetailHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = PROJECT_BG_COLOR;
        self.isService = NO;
        [self ma_addSubviews];
    }
    return self;
}

- (void)updateApplyStatusSigned:(BOOL)hasSigned {
    if (hasSigned) {
        [self.applyView updateImageName:@"finishedbutton_logo" title:@"已报名"];
    } else {
        [self.applyView updateImageName:@"apply_logo" title:@"报名"];
    }
    
    if (self.isService) {
        [self.applyView updateImageName:@"apply_logo" title:@"回复"];
    }
}

- (void)headerViewSetAllInfoWithModel:(MAMessageDetailModel *)model {
    NSLog(@"%@",model);
    self.titleLabel.text = model.sender_name;
    self.sendLabel.text = [NSString stringWithFormat:@"发送人：%@", model.sender_username];
    NSString *info = model.message_content;

    self.infoLabel.text = info;
    self.timeLabel.text = model.send_time;
    
    [self.imageContentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if ([model.message_image_count floatValue] > 0) {
        NSArray *imageArray = [model.message_images componentsSeparatedByString:@","];
        
        if (self.arrImage ==nil) {
            self.arrImage=[NSArray array];
        }
        self.arrImage=imageArray;
        CGFloat width = 108 * WIDTH_SCALE;
        CGFloat height = 68 * WIDTH_SCALE;
        int totalColumns = 3;
        CGFloat margin = 2;
    
        for(int i = 0; i < imageArray.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.backgroundColor = [UIColor lightGrayColor];
            
            int row = i / totalColumns;
            int col = i % totalColumns;
            //根据行号和列号来确定 子控件的坐标
            CGFloat cellX = col * (width + margin);
            CGFloat cellY = row * (height + margin);
            imageView.frame = CGRectMake(cellX, cellY, width, height);
            
            [self.imageContentView addSubview:imageView];
            
            NSString *imageUrl = [NSString stringWithFormat:@"%@%@", IMG_OSS, imageArray[i]];
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
            
            imageView.tag = i;
            imageView.userInteractionEnabled=YES;
            UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgclick:)];
            [imageView addGestureRecognizer:ges];
            
        }
        
        UIView *view = self.imageContentView.subviews.lastObject;
        [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@12);
            make.right.equalTo(@(-12));
            make.top.equalTo(self.infoLabel.mas_bottom).offset(12);
            make.bottom.equalTo(view.mas_bottom).priorityHigh();
        }];
        
    } else {
        [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@12);
            make.right.equalTo(@(-12));
            make.height.equalTo(@0);
            make.top.equalTo(self.infoLabel.mas_bottom).offset(0);
        }];
    }
    
//    if ([model.message_typechild isEqualToString:@"活动"] ||
//        [model.message_typechild isEqualToString:@"招生"] ||
//        [model.message_typechild isEqualToString:@"服务"] )
    if ([model.message_typechild isEqualToString:@"活动"] ||
        [model.message_typechild isEqualToString:@"招生"])
    {
        self.applyView.hidden = NO;
        self.adviceView.hidden = NO;
        [self.baseView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@4);
            make.right.equalTo(@(-4));
            make.top.equalTo(@4);
            make.bottom.equalTo(self.adviceView.mas_bottom).offset(12);
            make.bottom.equalTo(@0);
        }];
    } else {
        self.applyView.hidden = YES;
        self.adviceView.hidden = YES;
        [self.baseView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@4);
            make.right.equalTo(@(-4));
            make.top.equalTo(@4);
            make.bottom.equalTo(self.timeLabel.mas_bottom).offset(12);
            make.bottom.equalTo(@0);
        }];

    }
    
    if ([model.message_typechild isEqualToString:@"服务"]) {
        self.isService = YES;
    } else {
        self.isService = NO;
    }
    
    [self updateApplyStatusSigned:[model.b_signin boolValue]];

}

#pragma mark - private
- (void)ma_addSubviews {
    [self.contentView addSubview:self.baseView];
    
    [self.baseView addSubview:self.titleLabel];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.top.equalTo(@12);
    }];
    
    [self.baseView addSubview:self.sendLabel];
    [self.sendLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
    }];
    
    [self.baseView addSubview:self.infoLabel];
    [self.infoLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.top.equalTo(self.sendLabel.mas_bottom).offset(12);
        make.right.equalTo(@(-12));
    }];
    
    [self.baseView addSubview:self.imageContentView];
    [self.imageContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.right.equalTo(@(-12));
        make.height.equalTo(@0);
        make.top.equalTo(self.infoLabel.mas_bottom).offset(12);
    }];
    
    [self.baseView addSubview:self.timeLabel];
    [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageContentView.mas_bottom).offset(12);
        make.left.equalTo(@12);
    }];
    
    [self.baseView addSubview:self.applyView];
    [self.applyView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(87));
        make.top.equalTo(self.timeLabel.mas_bottom).offset(12);
        make.size.mas_offset(self.applyView.intrinsicContentSize);
    }];
    
    [self.baseView addSubview:self.adviceView];
    [self.adviceView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-87));
        make.top.equalTo(self.timeLabel.mas_bottom).offset(12);
        make.size.mas_offset(self.adviceView.intrinsicContentSize);
    }];
    
    [self.baseView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@4);
        make.right.equalTo(@(-4));
        make.top.equalTo(@4);
        make.bottom.equalTo(self.adviceView.mas_bottom).offset(12);
        make.bottom.equalTo(@0);
    }];
}

- (void)ma_tappedApply {
    !self.tapApplyBlock ?: self.tapApplyBlock();
}

- (void)ma_tappedAdvice {
    !self.tapAdviceBlock ?: self.tapAdviceBlock();
}

#pragma mark - getter
- (UIView *)baseView {
    if (!_baseView) {
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.layer.cornerRadius = 4;
    }
    return _baseView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:18];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}

- (UILabel *)sendLabel {
    if (!_sendLabel) {
        _sendLabel = [[UILabel alloc] init];
        _sendLabel.font = [UIFont systemFontOfSize:14];
        _sendLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _sendLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _sendLabel;
}

- (UILabel *)infoLabel {
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.font = [UIFont systemFontOfSize:14];
        _infoLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _infoLabel.textAlignment = NSTextAlignmentLeft;
        _infoLabel.numberOfLines = 0;
    }
    return _infoLabel;
}

- (UIView *)imageContentView {
    if (!_imageContentView) {
        _imageContentView = [[UIView alloc] init];
    }
    return _imageContentView;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.numberOfLines = 0;
    }
    return _timeLabel;
}

- (MAActionView *)applyView {
    if (!_applyView) {
        _applyView = [[MAActionView alloc] initWithImageName:@"apply_logo" title:@"报名"];
        __weak typeof(self) weakSelf = self;
        _applyView.clickBlock = ^{
            [weakSelf ma_tappedApply];
        };
    }
    return _applyView;
}

- (MAActionView *)adviceView {
    if (!_adviceView) {
        _adviceView = [[MAActionView alloc] initWithImageName:@"consultbutton_logo" title:@"咨询"];
        __weak typeof(self) weakSelf = self;
        _adviceView.clickBlock = ^{
            [weakSelf ma_tappedAdvice];
        };
    }
    return _adviceView;
}

-(void)imgclick:(UITapGestureRecognizer *)gestureRecognizer{
    
    UIImageView *tableGridImage = (UIImageView*)gestureRecognizer.view;
    NSInteger index = tableGridImage.tag;
    
    if ([self.delegate respondsToSelector:@selector(imgDidClick:)]) {
        [self.delegate imgDidClick:self.arrImage[index]];
    }
}

@end
